package com.ican.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.common.BusinessException;
import com.ican.common.hibernate.Page;
import com.ican.dao.user.RoleDao;
import com.ican.dao.user.RoleResourcesDao;
import com.ican.dao.user.UserRoleDao;
import com.ican.domain.user.Role;
import com.ican.domain.user.RoleResources;
import com.ican.domain.user.UserRole;

@Service("roleService")
public class RoleService {

    private RoleDao roleDao;
    private RoleResourcesDao roleResourcesDao;
    private UserRoleDao userRoleDao;

    public void addRole(Role role){
        if(roleDao.roleNameIsUnique(role.getName())){
            roleDao.addRole(role);
        }else{
            throw new BusinessException("角色名：" + role.getName() + "已经存在！");
        }
    }
    
    public Role findRoleById(Integer roleId){
        return roleDao.findRoleById(roleId);
    }
    
    /**
     * @param type    0:可用；1：不可用; 其他（-1）：全部
     */
    public List<Role> findAllRole(int type){
        return roleDao.findAllRole(type);
    }
    
    public Page<Role> findAllRole(Page<Role> page, Role role){
        return roleDao.findAllRole(page, role);
    }
    
    public void modifyRoleName(Role role){
        if(roleDao.roleNameIsUnique(role.getId(), role.getName())){
            roleDao.modifyRoleName(role.getId(), role.getName());
        }else{
            throw new BusinessException("角色名：" + role.getName() + "已经存在！");
        }
    }
    
    public void modifyRoleStatus(Integer roleId, byte dataStatus){
        roleDao.modifyRoleStatus(roleId, dataStatus);
    }
    
    public List<Role> findRoleByUserId(Integer userId){
        return roleDao.findRoleByUserId(userId);
    }
    
    public void addRoleResources(RoleResources rs){
        roleResourcesDao.addRoleResources(rs);
    }
    
    public void removeRoleResources(RoleResources rs){
        roleResourcesDao.removeRoleResources(rs);
    }
    
    public void addUserRole(UserRole ur){
        userRoleDao.addUserRole(ur);
    }
    
    public void removeUserRole(UserRole ur){
        userRoleDao.removeUserRole(ur);
    }
    
    @Resource
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }
    @Resource
    public void setRoleResourcesDao(RoleResourcesDao roleResourcesDao) {
        this.roleResourcesDao = roleResourcesDao;
    }
    @Resource
    public void setUserRoleDao(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }
}
